package com.sin.j2me.util;

public class ArrayList {
	private Object[] container = null;
	public static int DEFAULT_CAPACITY = 10;
	private int top = -1;
	public ArrayList() {
		this(DEFAULT_CAPACITY);
	}
	public ArrayList(int capacity) {
		this.container = new Object[capacity];
		this.top = 0;
	}
	public void clear(){
		for(int i=0; i<top; ++i){
			this.container[i] = null;
		}
		this.top = 0;
	}

	public boolean empty() {
		return this.top<=0;
	}
	
	public boolean full() {
		return this.container.length<=this.top;
	}
	
	private int newCapacity(){
		int newlen = this.container.length * 2;
		Object[] srcs = this.container;
		this.container = new Object[newlen];
		System.arraycopy(srcs, 0, container, 0, srcs.length);
		return newlen;
	}
	
	public int size() {
		return this.top;
	}
	
	public int add(Object obj) {
		if(full()){
			newCapacity();
		}
		this.container[this.top] = obj;
		++this.top;
		return this.top;
	}
	
	public int insert(Object obj, int index) {
		if(full()){
			newCapacity();
		}
		for(int i=top; i>index; --i){
			this.container[i] = this.container[i-1];
		}
		this.container[index] = obj;
		++this.top;
		return this.top;
	}
	public Object get(int ix){
		return container[ix];
	}
	
	public int delete(int ix){
		for(int i=ix; i<(top-1); ++i){
			this.container[i] = this.container[i+1];
		}
		--this.top;
		return this.top;
	}
}
